--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"

local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch, node_message)
	self.message_dispatch = message_dispatch	
	self.node_message = node_message
	self.port_socket = tonumber(skynet.getenv("port_socket"))
	self.port_websocket = tonumber(skynet.getenv("port_websocket"))
	self:register()
end

--注册本服务里的消息
function MessageHandler:register()

	self.message_dispatch:registerSelf('start', handler(self, self.start))
	self.message_dispatch:registerSelf('assignment_server', handler(self, self.assignmentServer))

	-- self.message_dispatch:registerSelf('message_to_user_nt', handler(self,self.messageToUserNt))
	-- self.message_dispatch:registerSelf('message_to_users_nt', handler(self,self.messageToUsersNt))
end




---------------------------------------------------------
-- CMD
---------------------------------------------------------
function MessageHandler:start()


	local maxclient = tonumber(skynet.getenv("maxclient"))
	local gate_num = tonumber(skynet.getenv("gate_num"))

    self.node_message = global.node_message
    self.server_info = global.server_info
    self.user_manager = global.user_manager
    self.cluster_manager = global.cluster_manager


	--skynet控制台
	-- skynet.newservice("debug_console",server_config.debug_port)

	local gate_service = skynet.newservice("gate_service")				
	skynet.call(gate_service, 'lua', 'start', pbc_env)
	--启动socket gate
	local conf = {
			port = self.port_socket,
			maxclient = maxclient,
			nodelay = true,
			watchdog = gate_service,
		}		
	local gate = skynet.newservice("gate")	
	skynet.call(gate, "lua", "open", conf)
	debug_log('_____________manager open socket gate port:'.. conf.port)

	--启动websocket gate
	local ws_conf = {
			port = self.port_websocket,
			maxclient = maxclient,
			nodelay = true,
			watchdog = gate_service,
		}
	local wsgate = skynet.newservice("wsgate")	
	skynet.call(wsgate, "lua", "open", ws_conf)
	debug_log('_____________manager open websocket gate port:'.. ws_conf.port)

	--socket消息处理
	-- local gate_service
	-- for i = 1, gate_num do
	-- 	gate_service = skynet.newservice("gate_service")				
	-- 	skynet.call(gate_service, 'lua', 'init', pbc_env)
	-- 	self.message_session:addGateService(gate_service)
	-- end


end


--给用户分配各类型服务器
function MessageHandler:assignmentServer(data)
	data = data or {}
	-- print("_________assignmentServer______", data)
	local res = {}
	local server_info = nil
	--1如果还有旧的节点继续用旧的
	--2旧的节点已失效给换一个新节点
	if not data.gate_server then 
		server_info = self.cluster_manager:getServerByType("gateserver")
		res.gate_server = server_info.name
	end
	if not data.hall_server then 
		server_info = self.cluster_manager:getServerByType("hallserver")
		res.hall_server = server_info.name
	end
	if not data.game_server then 
		server_info = self.cluster_manager:getServerByType("gameserver")
		res.game_server = server_info.name
	end
	if not data.node_server then 
		server_info = self.cluster_manager:getServerByType("nodeserver")
		res.node_server = server_info.name
	end
	-- print("_________assignmentServer_res_____", res)
	return res
end

-- --用户登出
-- function MessageHandler:onUserLogout(user_id, session_id)
-- 	--发送
-- 	local cmd = config.message_cmd
-- 	local session = self.message_session:getSession(session_id)
-- 	if not session then 
-- 		return 
-- 	end
-- 	local data = {
-- 		error_code = 0,
-- 		error_msg = "登出成功"	
-- 	}
-- 	self.node_message:sendClient(session.fd, session.contype, "logoutRes", data)
-- 	self.message_session:removeSession(session_id)
-- 	self.message_session:unbindSession(user_id)
-- end

-- --踢指定用户下线
-- function MessageHandler:onKickUser(fd)
-- 	local session = self.message_session:getSession(fd)
-- 	if session and session.service then 
-- 		debug_log("_____踢用户出线_______kick_user_fd_",fd)
-- 		self.node_message:sendService(session.service, "disconnect", fd)
-- 	end
-- 	-- if session.agent then 
-- 	-- 	self.node_message:sendService(session.agent, "kick", fd)
-- 	-- end
-- 	self.message_session:removeSession(fd)
-- end

-- --断线
-- function MessageHandler:onDisconnect(fd, user_id)
-- 	print("____onDisconnect___",fd, user_id)
-- 	--更新服务器信息到zone



-- --踢除指定会话
-- function MessageHandler:onKickSession(session_id)
-- 	local session = self.message_session:getSession(session_id)
-- 	if session then 
-- 		debug_log("_________MessageHandler:onKickSession___________session_id:",session_id)
-- 		self.node_message:sendService(session.service, "kick_connect", session_id)
-- 	end
-- end

-- --消息推送到user
-- function MessageHandler:messageToUserNt(user_id, message_name, msg_data)
--     -- print("____messageToUser____", message_name, msg_data)
-- 	local session = self.message_session:getSessionByUserId(user_id)
-- 	if not session then
-- 		return false
-- 	end	
-- 	-- print("____session____", session)	
-- 	self.node_message:sendClient(session.fd, session.contype, message_name, msg_data)
-- end

-- --消息推送到users
-- function MessageHandler:messageToUsersNt(users, message_name, msg_data)
--     print("____messageToUsers____", message_name, msg_data)
--     if type(users)=="table" then 
--     	--给多人转发消息
--     	for _,user_id in pairs(users) do 
-- 			local session = self.message_session:getSessionByUserId(user_id)
-- 			if session then
-- 				self.node_message:sendClient(session.fd, session.contype, message_name, msg_data)
-- 			end
-- 		end			
--     	return true
--     end
--     return false
-- end
return MessageHandler